/*
 * CustomerAction.java
 * 
 * Created on 02/10/2007, 12:43:14 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.actions;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Eleccion;
import model.factory.RecreacionalFactory;
import model.service.EleccionService;
import model.service.ReporteService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import view.common.ChartUtil;

import view.common.Constants;
import view.common.struts.CustomActionForm;

import view.common.struts.CustomDispatchAction;

/**
 * @Archivo LoginAction.java
 * @Fecha                @Autor   @Cambios
 * 21/01/2011 02:35:29 PM       moshiro
 */
public class ResultadoEleccionesAction extends CustomDispatchAction {

    private final static String PRINCIPAL = "principal";
    private final static String RESUMEN = "resumen";
    private final static String RESULTADOS = "resultados";
    private final static String PARTICIPANTES = "participantes";

    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        frm.setMapList(new HashMap());
        frm.getMapList().put("mensaje", ""); 
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward resumen(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Eleccion beanSearch = new Eleccion();
        beanSearch.setEstado(Constants.ESTADO_ACTIVO);
        if (getEleccionService().listarEleccion(beanSearch).size() == 0) {
            Map map = getReporteService().getResumenElecciones();
            frm.getMapList().put("resumen", map);
            frm.getMapList().put("fecha", new Date());
            return mapping.findForward(RESUMEN);
        } else {
            frm.getMapList().put("mensaje", "El proceso electoral se encuentra activo");
        }
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward resultados(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;

        Eleccion beanSearch = new Eleccion();
        beanSearch.setEstado(Constants.ESTADO_ACTIVO);
        if (getEleccionService().listarEleccion(beanSearch).size() == 0) {
            List<Map> list = getReporteService().getResultadoElecciones();
            frm.getMapList().put("resultados", list);

            List list_ = new ArrayList();
            String ganadorLogo = null;
            String ganadorNombre = null;
            String ganadorPorcentaje = null;
            int maxVotos = -1;
            int total = 0;

            for (Map map : list) {
                Map map_ = new HashMap();
                map_.put("category", map.get("Nombre"));
                map_.put("value", map.get("Porc"));
                list_.add(map_);
                if (maxVotos < Integer.parseInt("" + map.get("Votos"))) {
                    maxVotos = Integer.parseInt("" + map.get("Votos"));
                    ganadorLogo = "" + map.get("Logo");
                    ganadorNombre = "" + map.get("Nombre");
                    ganadorPorcentaje = "" + map.get("Porc");
                }
                total = total + Integer.parseInt("" + map.get("Votos"));
            }
            frm.getMapList().put("resultadosChart", list_);
            frm.getMapList().put("resultadosGanadorLogo", ganadorLogo);
            frm.getMapList().put("resultadosGanadorNombre", ganadorNombre);
            frm.getMapList().put("resultadosGanadorPorcentaje", ganadorPorcentaje);
            frm.getMapList().put("resultadosTotal", total);

            return mapping.findForward(RESULTADOS);
        } else {
            frm.getMapList().put("mensaje", "El proceso electoral se encuentra activo");
        }
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward participantes(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Eleccion beanSearch = new Eleccion();
        beanSearch.setEstado(Constants.ESTADO_ACTIVO);
        if (getEleccionService().listarEleccion(beanSearch).size() == 0) {
            Map map = getReporteService().getParticipacionElecciones();
            frm.getMapList().put("participantes", map);
            return mapping.findForward(PARTICIPANTES);
        } else {
            frm.getMapList().put("mensaje", "El proceso electoral se encuentra activo");
        }
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward comparativoParticipantes(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Map map = (Map) frm.getMapList().get("participantes");
        List list = new ArrayList();
        Map map_ = new HashMap();
        map_.put("category", "PARTICIPACION");
        map_.put("value", map.get("PorcAsistentes"));
        list.add(map_);
        map_ = new HashMap();
        map_.put("category", "AUSENTISMO");
        map_.put("value", map.get("PorcVotosAusentes"));
        list.add(map_);

        JFreeChart grafica = ChartUtil.createPieChart3D(list, "");
        return chart("comparativoParticipantes", grafica, mapping, form, req, resp);
    }

    public ActionForward comparativoResultados(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        List<Map> list = (List<Map>) frm.getMapList().get("resultadosChart");

        JFreeChart grafica = ChartUtil.createPieChart3D(list, "");
        return chart("comparativoParticipantes", grafica, mapping, form, req, resp);
    }

    private ActionForward chart(String nombre, JFreeChart grafica, ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        resp.setContentType("image/jpeg");
        OutputStream salida = resp.getOutputStream();
        CustomActionForm frm = (CustomActionForm) form;
        frm.getMapList().put(nombre, grafica);
        String width = req.getParameter("width");
        String height = req.getParameter("height");
        ChartUtilities.writeChartAsJPEG(salida, grafica, Integer.parseInt(width != null ? width : "350"), Integer.parseInt(height != null ? height : "250"));
        salida.close();
        return null;
    }

    public ReporteService getReporteService() {
        return RecreacionalFactory.getInstance().getReporteService();
    }

    public EleccionService getEleccionService() {
        return RecreacionalFactory.getInstance().getEleccionService();
    }
}
